//
//  RNOLSettingsViewController.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/11.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLSettingsViewController.h"
#import "RNOLStaticSettingsCell.h"
#import "RNOLAccountSecurityViewController.h"
#import "RNOLMessageNotificationViewController.h"

#define kStaticSettingsCellID  @"RNOLStaticSettingsCell"

@interface RNOLSettingsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSDictionary *dataDict;

@end

@implementation RNOLSettingsViewController

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , KHIGHT) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        adjustsScrollViewInsets_NO(_tableView, self);
//        _tableView.height -= kBottomHeight;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone ;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName: kStaticSettingsCellID bundle:nil] forCellReuseIdentifier: kStaticSettingsCellID];
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , 10)];
        header.backgroundColor = kBgColor;
        _tableView.tableHeaderView = header;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview: _tableView];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBarTitleViewWithText:@"设置"];
    self.view.backgroundColor = kBgColor;
    [self setup];
}

- (void)setup{
    self.dataDict = @{
                      @"0" : @[@"账号与安全",@"新消息通知",@"隐私"],
                      @"1" : @[@"退出登录"]
                      };
    [self.tableView reloadData];
}

#pragma mark -  UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.dataDict.allKeys count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *items = self.dataDict[@(section).stringValue];
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RNOLStaticSettingsCell *cell = [tableView dequeueReusableCellWithIdentifier: kStaticSettingsCellID];
    NSArray *items = self.dataDict[@(indexPath.section).stringValue];
    cell.staticTitleLabel.text = items[indexPath.row] ;

    if (indexPath.section == 1) {
        [cell removeAllSubviews];
        UIView *cellContentView = [[UIView alloc] initWithFrame:cell.bounds];
         cell.backgroundColor = kBgColor;
        [cell addSubview: cellContentView];
        UILabel *label = [RNOLUnitTool createLabelWithText: items[indexPath.row] textColor: kTextColor fontSize: 18 textAlignment:NSTextAlignmentCenter];
        label.backgroundColor = KWhiteColor;
        label.frame = CGRectMake(20, 0, KWIDTH - 40 , 44);
        [cellContentView addSubview: label];
    }
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section? 50 : 60 ;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , 40)];
    footer.backgroundColor = kBgColor;
    return footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *items = self.dataDict[@(indexPath.section).stringValue];
    NSString *title = [items objectAtIndex: indexPath.row];
    if ([title isEqualToString:@"账号与安全"]) {
        RNOLAccountSecurityViewController *accountSecurityVC = [[RNOLAccountSecurityViewController alloc] init];
        [self.navigationController pushViewController: accountSecurityVC animated:YES];
    }else if([title isEqualToString:@"新消息通知"]){
        RNOLMessageNotificationViewController *messageVC = [[RNOLMessageNotificationViewController alloc] init];
        [self.navigationController pushViewController: messageVC animated:YES];
        
    }else if([title isEqualToString:@"隐私"]){
        
        
    }else if([title isEqualToString:@"退出登录"]){
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"融牛提示您" message:@"是否确认要退出登录?" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // login out
            [self showStatusTipWithText:@"正在退出登录..."];
            
        }];
        [alertVC addAction: cancelAction];
        [alertVC addAction: confirmAction];
        [self presentViewController: alertVC animated:YES completion:^{
            
        }];
    }
}


@end
